package com.study.happystudy.service.impl;

import com.study.happystudy.controller.CourseController;
import com.study.happystudy.mapper.CourseMapper;
import com.study.happystudy.mapper.UserMapper;
import com.study.happystudy.po.Course;
import com.study.happystudy.po.User;
import com.study.happystudy.service.CourseService;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    CourseMapper courseMapper;
    @Resource
    UserMapper userMapper;

    private Course toCourse(Course c) {
        String teacherAvatar = userMapper.findUserById(c.getUserId()).getUserHeadPortrait();
        c.setTeacherAvatar(teacherAvatar);
        return c;
    }

    private List<Course> toCourseList(List<Course> courses) {
        return courses.stream().map(this::toCourse).toList();
    }

    public int isUserOrCourseExist(int userId, int courseId) {
        User u = userMapper.findUserById(userId);
        Course c = courseMapper.findCourseById(courseId);

        //课程找不到返回-1,用户找不到返回-2,都他吗找不到返回-3
        if (u == null && c == null) return -3;
        if (c == null) return -1;
        if (u == null) return -2;

        //正确代码:1
        return 1;
    }

    // todo: 记得完善一下
    @Override
    public boolean insertCourse(
            int userId,
            String courseName,
            String courseDescription,
            String imgUrl,
            String startTime,
            String endTime,
            int duration,
            int grade,
            int subjectId,
            String classTime,
            String type
    ) {

        try {
            courseMapper.insertCourse(userId, courseName, courseDescription, imgUrl, startTime, endTime, duration, grade, subjectId, classTime,type);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public boolean deleteCourseById(int courseId) {
        Course c = courseMapper.findCourseById(courseId);
        if (c == null) return false;// 不存在该课程则返回false
        try {
            courseMapper.deleteCourseById(courseId);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateCourse(int courseId, String courseName, String courseDescription, String imgUrl,
                                String startTime, String endTime, int duration, int grade, int subjectId, String classTime,String type) {
        Course c = courseMapper.findCourseById(courseId);
        if (c == null) return false;// 不存在该课程则返回false
        courseMapper.updateCourse(courseId, courseName, courseDescription, imgUrl, startTime, endTime, duration, grade, subjectId, classTime,type);
        return true;
    }

    @Override
    public List<User> selectStudentsByCourseId(int courseId) {
        try {
            return courseMapper.selectStudentsByCourseId(courseId);
        } catch (Exception e) {
            return null;
        }
    }

    public int insertStudentToCourse(int courseId, int userId) {

        int res = isUserOrCourseExist(userId, courseId);
        if (res != 1) return res;

        int cnt = courseMapper.isExist(courseId, userId);
        if (cnt == 1) return -3;

        courseMapper.insertStudentToCourse(courseId, userId);
        return 1;
    }

    @Override
    public int deleteStudentFromCourse(int courseId, int userId) {

        int res = isUserOrCourseExist(userId, courseId);
        if (res != 1) return res;

        int cnt = courseMapper.isExist(courseId, userId);
        if (cnt == 0) return -3;
        courseMapper.deleteStudentFromCourse(courseId, userId);
        return cnt;
    }

    @Override
    public List<Course> selectCoursesBySubject(int subjectId) {
        return toCourseList(courseMapper.selectCoursesBySubject(subjectId));
    }

    @Override
    public List<Course> selectCoursesByGradeAndSubject(int grade, int subjectId) {
        return toCourseList(courseMapper.selectCoursesByGradeAndSubject(grade, subjectId));
    }

    @Override
    public List<Course> selectCoursesByGrade(int grade) {
        return toCourseList(courseMapper.selectCoursesByGrade(grade));
    }

    @Override
    public List<Course> searchByTerm(String searchTerm) {
        return toCourseList(courseMapper.searchByTerm(searchTerm));
    }

    @Override
    public Course findCourseById(int courseId) {
        return toCourse(courseMapper.findCourseById(courseId));
    }

    @Override
    public List<Course> searchCourseByTeacherName(String teacherName) {
        return toCourseList(courseMapper.searchCourseByTeacherName(teacherName));
    }

    @Override
    public void collectCourses(int userId, int courseId) {
        courseMapper.collectCourses(userId, courseId);
    }

    @Override
    public List<Course> getUserFavorites(int userId) {
        return toCourseList(courseMapper.getUserFavorites(userId));
    }

    public List<Course> getUserFavoritesBySubject(@Param("userId") int userId, @Param("subjectId") int subjectId){
        return toCourseList(courseMapper.getUserFavoritesBySubject(userId, subjectId));
    }
    //返回用户收藏的所有课程根据年级
    public List<Course> getUserFavoritesByGrade(@Param("userId") int userId, @Param("grade") int grade){
        return toCourseList(courseMapper.getUserFavoritesByGrade(userId, grade));
    }
    //    返回用户收藏的所有课程根据根据年级和科目
    public List<Course> getUserFavoritesBySubjectAndGrade(@Param("userId") int userId, @Param("subjectId") int subjectId, @Param("grade") int grade){
        return toCourseList(courseMapper.getUserFavoritesBySubjectAndGrade(userId, subjectId, grade));
    }

    @Override
    public boolean isFavorites( int userId,int courseId) {
        return courseMapper.isFavorites(userId, courseId);
    }

    @Override
    public void cancelCollectCourses(int userId, int courseId) {
        courseMapper.cancelCollectCourses(userId, courseId);
    }

    public List<Course> getAllCourses() {
        return toCourseList(courseMapper.getAllCourses());
    }
    public List<Course> selectCoursesByUserId(int userId){
        return toCourseList(courseMapper.selectCoursesByUserId(userId));
    }
}
